package ui;

import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.*;

import project_package.Employee;
import controller.EmployeeController;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;

public class LoginMenu implements ActionListener 
{
	private static LoginMenu instance = null;
	private static Employee employee;
    private EmployeeController empCtrl;	
    private JButton loginButtonLogin;
	private static JFrame frmLoginUi;
	private JTextField loginTextFieldUsername;
	private JPasswordField loginTextFieldPassword;
	
	public static LoginMenu getInstance()
    {
    	if(instance == null)
    	{
    		instance = new LoginMenu();
    	}
    	return instance;
    }
	
    public LoginMenu() 
    {
    	
    	empCtrl = new EmployeeController();
    	frmLoginUi = new JFrame("Employee UI");
    	frmLoginUi.setTitle("Login UI");
    	
    	JLabel loginLabelUsername = new JLabel("Username");
    	JLabel loginLabelPassword = new JLabel("Password");

    	loginTextFieldUsername = new JTextField(10);
    	loginTextFieldPassword = new JPasswordField(10);
    	loginButtonLogin = new JButton("Log in");
    	loginButtonLogin.addActionListener(this);
    	
    	GroupLayout groupLayout = new GroupLayout(frmLoginUi.getContentPane());
    	groupLayout.setHorizontalGroup(
    		groupLayout.createParallelGroup(Alignment.LEADING)
    			.addGroup(groupLayout.createSequentialGroup()
    				.addGap(64)
    				.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
    					.addComponent(loginLabelUsername)
    					.addComponent(loginLabelPassword))
    				.addGap(18)
    				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
    					.addComponent(loginButtonLogin)
    					.addComponent(loginTextFieldPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    					.addComponent(loginTextFieldUsername, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    				.addContainerGap(77, Short.MAX_VALUE))
    	);
    	groupLayout.setVerticalGroup(
    		groupLayout.createParallelGroup(Alignment.LEADING)
    			.addGroup(groupLayout.createSequentialGroup()
    				.addGap(50)
    				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
    					.addComponent(loginLabelUsername)
    					.addComponent(loginTextFieldUsername, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    				.addPreferredGap(ComponentPlacement.UNRELATED)
    				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
    					.addComponent(loginTextFieldPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    					.addComponent(loginLabelPassword))
    				.addPreferredGap(ComponentPlacement.UNRELATED)
    				.addComponent(loginButtonLogin)
    				.addContainerGap(37, Short.MAX_VALUE))
    	);
    	frmLoginUi.getContentPane().setLayout(groupLayout);
		empCtrl = new EmployeeController();
    }
    
    public void display()
	{
		frmLoginUi.getContentPane();
		frmLoginUi.setSize(300,200);
		frmLoginUi.setVisible(true);
		frmLoginUi.setResizable(false);
	}
    
	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource() == loginButtonLogin)
		{
			HashMap<Integer, Employee> employees = empCtrl.getEmployees();
			@SuppressWarnings("deprecation")
			String password = loginTextFieldPassword.getText();
        	String username = loginTextFieldUsername.getText();
        	
        	// admin account
        	empCtrl.createEmployee("admin", "+555 555 555", "Aalborg, Vesterbro 11", 1111112222, 1337.0, true, "admin", 0.0, 0);
        	Set<Map.Entry<Integer, Employee>> st = employees.entrySet();
        	
        	if ((username.length() > 0) && (password.length() > 0)) 
        	{
            	for(Map.Entry<Integer, Employee> employee : st)
            	{
            		if (employee.getValue().getName().equals(username))
		        	{
		        		if(employee.getValue().getPassword().equals(password)) 
		        		{
		        			//JOptionPane.showMessageDialog(null, "You have logged in as "+username, "Success", 1);
		        			LoginMenu.setEmployee(employee.getValue());
		        			frmLoginUi.setVisible(false);
		        			ui.MainUI.main (null);
		        		}
		        		else {
		        			JOptionPane.showMessageDialog(null, "Wrong password", "Error", 0);
		        		}
		        	} else {
		        		JOptionPane.showMessageDialog(null, "Wrong username", "Error", 0);
		        			}
		        	
	            	 	
            	}
        	}
        	else {
        		JOptionPane.showMessageDialog(null,"You have left empty fields");        		           		
        	}
		}
	}
	
	public static void logOut() {
		employee = null;
		frmLoginUi.setSize(300,200);
		frmLoginUi.setVisible(true);
		frmLoginUi.setResizable(false);
	}
	       
    public static void setEmployee(Employee employeee) 
    {
    	employee = employeee;		
    }
    
    public static Employee getEmployee() 
    {
    	return employee;
    }
    
    public boolean checkEmployee()
    {
    	return employee.getIsManager();
    }

	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
    {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		LoginMenu loginMenu = new LoginMenu();
		loginMenu.display();
    }
}